package org.xlin.biz.controller.rest.menu;

import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.xlin.biz.service.menu.MenuService;
import org.xlin.biz.util.ResponseUtil;

/**
 * 
 * 菜单信息异步请求处理类
 * @author xl
 *
 */
@RestController
@RequestMapping("/menu")
public class MenuController {
	
	Logger logger = LoggerFactory.getLogger(MenuController.class);
	
	@Resource
	MenuService menuService;
	
	/**
	 * 分页查询菜单列表
	 * @param pageable
	 * @param condition
	 * @return
	 */
    @RequestMapping(value="/list",method = RequestMethod.POST)
    public ResponseEntity<?> queryRecord4Page(@PageableDefault Pageable pageable,
			@RequestParam Map<String,Object> condition){
		logger.debug("分页查询菜单列表：{}",condition);
		try {
			return ResponseUtil.json(menuService.queryRecord4Page(pageable, condition), HttpStatus.OK);
		} catch (Exception e) {
			logger.error("分页查询菜单列表失败：{}",e);
			return ResponseUtil.text("查询失败！",HttpStatus.INTERNAL_SERVER_ERROR);
		}
	}
    
    /**
	 * 增加菜单
	 * @param condition
	 * @return
	 */
    @RequestMapping(value="/oper",method = RequestMethod.POST)
	public ResponseEntity<?> addRecord(@RequestBody Map<String,Object> condition){
		logger.debug("增加菜单：{}",condition);
		try {
			menuService.addRecord(condition);
			return ResponseUtil.text("保存成功！",HttpStatus.OK);
		} catch (Exception e) {
			logger.error("增加菜单失败：{}",e);
			return ResponseUtil.text("保存失败！",HttpStatus.INTERNAL_SERVER_ERROR);
		}
	}
	
	/**
	 * 更新菜单
	 * @param condition
	 * @return
	 */
    @RequestMapping(value="/oper",method = RequestMethod.PUT)
	public ResponseEntity<?> modfiyRecord(@RequestBody Map<String,Object> condition){
		logger.debug("更新菜单：{}",condition);
		try {
			int count=menuService.modfiyRecord(condition);
			if(count>0){
				return ResponseUtil.text("更新成功！", HttpStatus.OK);
			}
			return ResponseUtil.text("更新失败，记录不存在。", HttpStatus.INTERNAL_SERVER_ERROR);
		} catch (Exception e) {
			logger.error("更新菜单失败：{}",e);
			return ResponseUtil.text("更新失败！",HttpStatus.INTERNAL_SERVER_ERROR);
		}
	}
	
	/**
	 * 删除菜单
	 * @param condition
	 * @return
	 */
    @RequestMapping(value="/oper",method = RequestMethod.DELETE)
	public ResponseEntity<?> deleteRecord(@RequestParam Map<String,Object> condition){
		logger.debug("删除菜单：{}",condition);
		try {
			menuService.deleteRecord(condition);
			return ResponseUtil.text("删除成功！",HttpStatus.OK);
		} catch (Exception e) {
			logger.error("删除菜单失败：{}",e);
			return ResponseUtil.text("删除失败！",HttpStatus.INTERNAL_SERVER_ERROR);
		}
	}
    
    /**
     * 查询就餐记录菜单
     * @param repastId
     * @return
     */
    @RequestMapping(value="/repast",method=RequestMethod.GET)
    public ResponseEntity<?> queryMenusByRepastId(@RequestParam int repastId){
    	logger.debug("查询就餐记录菜单：{}",repastId);
		try {
			return ResponseUtil.json(menuService.queryMenusByRepastId(repastId), HttpStatus.OK);
		} catch (Exception e) {
			logger.error("查询就餐记录菜单失败：{}",e);
			return ResponseUtil.text("查询就餐记录菜单失败，请联系管理员。",HttpStatus.INTERNAL_SERVER_ERROR);
		}
    }
    
    /**
     * 查询所有菜单
     * @param repastId
     * @return
     */
    @RequestMapping(value="/all",method=RequestMethod.GET)
    public ResponseEntity<?> queryAllMenus4LoadSels(){
    	logger.debug("查询所有菜单");
		try {
			return ResponseUtil.json(menuService.queryAllMenus4LoadSels(), HttpStatus.OK);
		} catch (Exception e) {
			logger.error("查询所有菜单失败：{}",e);
			return ResponseUtil.text("查询所有菜单失败，请联系管理员。",HttpStatus.INTERNAL_SERVER_ERROR);
		}
    }

}
